package com.arceus.leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author : iron
 * @version : 1.0.0
 * @date : 3:29 PM 2018/12/20
 */

public class T637 {

    public static void main(String[] args) {

    }

    public List<Double> averageOfLevels(SameTree.TreeNode root) {
        List<Double> res = new ArrayList<Double>();
        if(root == null)
            return res;
        Queue<SameTree.TreeNode> queue = new LinkedList<SameTree.TreeNode>();
        queue.add(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            int count = size;
            double sum = 0.0;
            while(size > 0){
                SameTree.TreeNode node = queue.poll();
                sum += node.val;
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
                size--;
            }
            res.add(sum/count);
        }
        return res;
    }






}
